﻿//-------------------------------------------------------------------------------------------
// <copyright file="OfferConverter.cs" company="Russell Wilkins">
//     Copyright (c) Russell Wilkins 2014. All Rights Reserved.
// </copyright>
// <author>Russell Wilkins</author>
// <license href="license.txt">
//     The use of this software is governed by the Microsoft Public License
//     which is included with this distribution.
// </license>
//-------------------------------------------------------------------------------------------
namespace Ripple.Data.Converters
{
    using Helpers;
    using Providers;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    /// <summary>
    /// TODO: Describe the class
    /// </summary>
    internal static class OfferConverter
    {
        internal static IEnumerable<Offer> ToEntities(this IEnumerable<AccountOfferData> list, IRippleContext rippleContext, string accountId)
        {
            foreach (var item in list)
            {
                yield return item.ToEntity(rippleContext, accountId);
            }
        }

        internal static Offer ToEntity(this AccountOfferData data, IRippleContext rippleContext, string accountId)
        {
            if (data == null)
            {
                return null;
            }

            Offer entity = new Offer(rippleContext)
            {
                AccountId = accountId,
                Gets = new CurrencyAmount(),
                Pays = new CurrencyAmount()
            };
            entity.UpdateEntity(data);
            return entity;
        }

        internal static void UpdateEntity(this Offer entity, AccountOfferData data)
        {
            if (data == null)
            {
                return;
            }

            entity.Sequence = data.seq;

            if (data.taker_gets is string)
            {
                entity.Gets.Currency = "XRP";
                entity.Gets.Value = (data.taker_gets as string).ToDouble();
            }
            else
            {
                Dictionary<string, object> currency = (Dictionary<string, object>)data.taker_gets;
                entity.Gets.Currency = currency["currency"] as string;
                entity.Gets.Value = (currency["value"] as string).ToDouble();
                entity.Gets.Issuer = currency["issuer"] as string;
            }

            if (data.taker_pays is string)
            {
                entity.Pays.Currency = "XRP";
                entity.Pays.Value = (data.taker_pays as string).ToDouble();
            }
            else
            {
                Dictionary<string, object> currency = (Dictionary<string, object>)data.taker_pays;
                entity.Pays.Currency = currency["currency"] as string;
                entity.Pays.Value = (currency["value"] as string).ToDouble();
                entity.Pays.Issuer = currency["issuer"] as string;
            }

            entity.Updated = DateTime.Now;
        }
    }
}